package com.grwx.omc.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grwx.omc.entity.*;
import com.grwx.omc.admin.dao.UserMapper;
import com.grwx.omc.admin.entity.SysUser;
import com.grwx.omc.admin.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseData login(String userCode, String password) {
        //查询用户信息
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("user_code",userCode);
        SysUser sysUser = userMapper.selectOne(sysUserQueryWrapper);
        if(null!=sysUser&& BCrypt.checkpw(password,sysUser.getPassword())) {
            //设置令牌信息
            Map<String,Object> info = new HashMap<String,Object>();
            info.put("role","USER");
            info.put("userId",sysUser.getId());
            info.put("username",sysUser.getUserName());
            //生成令牌
            String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), JSON.toJSONString(info),null);
            return ResponseData.success("omc.admin.auth.log.success",jwt);
        }
        return ResponseData.error("omc.admin.auth.log.failure");
    }
}
